<template>
  <div class="layout-all">
    <control :config="config" />
    <div class="layout-header" style="float:left">
      <router-link to="./form/">
        <el-button
          type="primary"
          icon="el-icon-plus"
          style="margin-left:0px"
        >添加</el-button>
      </router-link>
    </div>
    <div class="layout-content">
      <el-table
        :data="tableData"
        stripe
        align="center"
        highlight-current-row
        style="width: 100%;margin-top:50px"
      >
      <el-table-column align="center" prop="id" label="序号" width="70" :fixed="isMobile()?'left':undefined"/>
      <el-table-column align="center" prop="type" label="类型" width="100">
          <template slot-scope="scope">
            <el-tag
              :type="scope.row.type %2==0 ? 'primary' : 'success'"
              size="mini"
            >{{ type_arr[scope.row.type] }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="title" label="团购名称" width="150"/>
        <el-table-column align="center" prop="thumb" label="封面" width="100">
          <template slot-scope="scope">
            <pre-view :src="scope.row.thumb" />
          </template>
        </el-table-column>

         <el-table-column align="center" prop="start_time" label="开始时间" width="180" >
          <template slot-scope="scope">
            <el-tag type="danger" size="mini" v-if="isGreaterNow(scope.row.start_time)">未开始</el-tag>
            {{scope.row.start_time}}
          </template>
        </el-table-column>
        <el-table-column align="center" prop="end_time" label="结束时间" width="180" >
          <template slot-scope="scope">
             <el-tag type="danger" size="mini" v-if="!isGreaterNow(scope.row.end_time)">已结束</el-tag>
            {{scope.row.end_time}}
          </template>
        </el-table-column>
        <el-table-column align="center" prop="group_time" label="拼团时间(小时)"  />
        <el-table-column align="center" prop="real_price" label="价格"  />
        <el-table-column align="center" prop="people_count" label="团购人数"  />
        <el-table-column align="center" prop="group_count" label="成团数" />
        <el-table-column align="center" prop="max_group_count" label="最大成团数" />
        <el-table-column align="center" prop="stock" label="库存" width="70" />
         <el-table-column align="center" prop="stock" label="记录" width="70" >
          <template slot-scope="scope" >
            <el-button type="primary" size="small" icon="el-icon-edit-outline" circle  @click="stock_log_click(scope.row)"></el-button>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="status" label="是否启用" width="80">
          <template slot-scope="scope">
            <el-button
              :type="scope.row.status === 1 ? 'primary' : 'success'"
              size="mini"
              @click="change_status(scope.row.id,'status' ,scope.row.status)"
            >{{ scope.row.status === 1 ? "是" : "否" }}</el-button>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="telephone"
          :fixed="isMobile()?undefined:'right'"
          width="240"
          label="操作"
        >
          <template slot-scope="scope">
            <el-button
              size="small"
              type="success"
              icon="el-icon-s-tools"
              @click="stock_click(scope.row)"
            />
            <el-button size="small" type="warning" icon="el-icon-circle-plus" @click="open(scope.row)"/>
            <router-link :to="'./form/' + scope.row.id">
              <el-button size="small" type="primary" icon="el-icon-edit" />
            </router-link>
            
            <el-button
              size="small"
              type="danger"
              icon="el-icon-delete"
              @click="onDel(scope.row.id)"
            />
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-pagination
      :current-page="current_page"
      :page-size="per_page"
      layout="total, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <!-- 规格库存 -->
    <drawer title="操作规格库存" :isShow="spec_dialog"  @update:isShow="spec_dialog=false" :width="'.5'">
      <spec-panel v-if="spec_dialog" :id="goods_id" :is_spec="is_spec" :group="true" @close="stock_close" />
    </drawer>
    <drawer title="库存记录" :isShow="stoc_visible"  @update:isShow="stoc_visible=false" width=".3">
      <stockLog v-if="stoc_visible" :id="goods_id"/>
    </drawer>
  </div>
</template>

<script>
import control from '../../components/control/control'
import SpecPanel from '@/components/orderlist/SpecPanel'
import stockLog from '@/components/orderlist/stockLog'
export default {
  name:"group_buy_index",
  components: { control ,SpecPanel,stockLog},
  data() {
    return {
      thumb_url: "",
      spec_id:'',
      type_arr:[],
      goods_id: 0,
      config: [
        { type: 'text', placeholder: '请输入团购名称', field: 'title', val: '' }
      ],
      value: '',
      searchWhere: {
        title: ''
      },
      tableData: [],
      total: 0,
      current_page: 1,
      per_page: 14,
      spec_dialog: false,
      stoc_visible: false,

      spec_tableData: []
    }
  },
  activated(){
    if(this.$getUpdate()){
      this.onSearch()
    }
  },
  created() {
    this.onSearch()
  },
  methods: {
    isGreaterNow(time){
      return new Date(time)>new Date()
    },
    open(row){

      this.$prompt('请输入虚拟人数', '新建一个拼团', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
        }).then(({ value }) => {
          if(value==undefined||value<1){
            this.$msg_ok('error','请输入虚拟人数')
            return
          }
          if(value>=row.max_people_count-row.people_count-row.type){
            this.$msg_ok('error','虚拟人数过大')
            return
          }
          this.get('/shop/GoodsGroup/create', {id:row.id,value:value}, 'POST').then(e => {
            this.$msg_ok('success','创建完成')
          })
        })
    },
    // 查询
    handleSizeChange(val) {
      this.onSearch()
    },
    handleCurrentChange(val) {
      this.current_page = val
      this.onSearch()
    },
    onSearch() {
      const form = {}
      form.page = this.current_page
      form.map = this.searchWhere
      this.get('/shop/GoodsGroup/index', form, 'POST').then(e => {
        this.tableData = e.list.data
        this.per_page = e.list.per_page
        this.type_arr = e.type_arr
        this.total = e.list.total
        this.current_page = e.list.current_page
      })
    },
    // 添加
    onAdd() {},
    // 编辑
    onEdit(id) {
      console.log(id)
    },
    // 修改状态
    change_status(id, field, val) {
      this.update(id, val, field, 'GoodsGroup','shop').then(e => {
        e ? this.onSearch() : ''
      })
    },
    // 删除
    onDel(id) {
      this.$msg_confirm().then(e => {
        if (e) {
          this.get('/shop/GoodsGroup/del?id=' + id, this.form, 'POST').then(e => {
            this.onSearch()
          })
        }
      })
    },
    stock_log_click(row) {
      this.goods_id=row.goods_id
      this.stoc_visible=true
    },
    //打开规格
    stock_click(row) {
      this.goods_id = row.goods_id
      this.is_spec = row.is_spec
      this.spec_dialog = true;
    },
    //规格保存完成
    stock_close(type=0){
      this.spec_dialog = false;
      if(type==0)
      this.onSearch();
    },

    change_status2(id, field, val) {
      const form = {}
      form.id = id
      form.field = field
      val === 1 ? form.val = 0 : form.val = 1
      this.get('/shop/GoodsGroup/spec_status', form, 'post').then(e => {
        this.$msg_ok('success', '修改成功')
        this.spec_info(this.goods_id)
      })
    },
    thumbClick(id, goods_id) {
      this.spec_id = id;
      this.goods_id = goods_id;
    },
    // 上传成功
    handle_thumb(res, file) {
      const index = this.spec_tableData.findIndex((e) => e.id === this.spec_id);
      this.spec_tableData[index].thumb = file.response.data.path;
    },
  }
}
</script>

<style>
.layout-all {
  margin-top: 20px;
  margin-left: 10px;
}
.layout-content {
  margin-top: 20px;
}
</style>>
